package yby.jdbc;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLPortType;

import java.nio.channels.ClosedSelectorException;
import java.sql.*;
import java.util.Scanner;

public class AppLogin {
        public static void main(String[] args) throws Exception{
            System.out.println("请输入账号:");
            Scanner scanner = new Scanner(System.in);
            String username = scanner.nextLine();

            System.out.println("请输入密码:");
            String password = scanner.nextLine();

            loginplus(username,password);

        }

        public static void login (String u,String p)throws Exception{
            System.out.println("输入的账号是:" + u);
            System.out.println("输入的密码是:" + p);
            String sql="SELECT id FROM `user` WHERE username='" +u+
                    "' AND `password`=MD5('"
                    + p+
                    "')";
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/test?useSSL=false";
            String username="root";
            String password="root";
            Connection connection= DriverManager.getConnection(url,username,password);
            Statement statement=connection.createStatement();
            ResultSet set=statement.executeQuery(sql);
            if(set.next()) System.out.println("登陆成功");
            else System.out.println("登陆失败");
        }

    public static void loginplus(String u,String p) throws Exception{
        System.out.println("输入的账号是:" + u);
        System.out.println("输入的密码是:" + p);
        String sql="SELECT id FROM `user` WHERE username= ? AND `password`=MD5(?)";
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/test?useSSL=false";
        String username="root";
        String password="root";
        Connection connection= DriverManager.getConnection(url,username,password);

        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,u);
        ps.setString(2,p);
        ResultSet set = ps.executeQuery();
        if(set.next()) System.out.println("登陆成功");
        else System.out.println("登陆失败");

    }

}
